package m202401.d02;

import java.util.*;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/2 09:00
 */
//49. 字母异位词分组
public class Q49 {


    int[] map = new int['z' - 'a' + 1];

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> rsMap = new HashMap<>();
        for (String str : strs) {
            String key = getKey(str);
            if (!rsMap.containsKey(key)) {
                List<String> list = new ArrayList<>();
                list.add(str);
                rsMap.put(key, list);
            } else {
                rsMap.get(key).add(str);
            }
        }
        return new ArrayList<>(rsMap.values());
    }

    private String getKey(String s) {
        Arrays.fill(map, 0);
        for (int i = 0; i < s.length(); i++) {
            map[s.charAt(i) - 'a'] += 1;
        }
        return Arrays.toString(map);
    }
}
